/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.biblioteca.util;


import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Andrea Di Grazia, Massimiliano Giovine
 * @date 29-set-2008
 * @file UtilFileOperation.java
 */
public class UtilFileOperation {

    public static ArrayList <Book> getBookList() throws FileNotFoundException{
        ArrayList <Book> book_list = new ArrayList();
        
        XMLDecoder decode_xml = new XMLDecoder(
                    new BufferedInputStream(
                    new FileInputStream("./dataBase.xml")));

       
        while(true){
            try{
            Book book = (Book) decode_xml.readObject();
            book_list.add(book);
            System.out.println("Libro letto: "+book.getTitle());
            }
            catch(ArrayIndexOutOfBoundsException ex){
                return book_list;
            }
            
        }
        
    }
    
    public static ComboFields getComboFields() throws FileNotFoundException{
        ComboFields f  = new ComboFields();
        ArrayList <String> fields = new ArrayList();
        
        XMLDecoder decode_xml = new XMLDecoder(
                    new BufferedInputStream(
                    new FileInputStream("./comboFields.xml")));

        
        while(true){
            try{
            String field = (String) decode_xml.readObject();
            fields.add(field);
            System.out.println("Campo estratto: "+field);
            }
            catch(ArrayIndexOutOfBoundsException ex){
                f.setFields(fields);
                return f;
            }
            
        }
        
    }

    public static void saveDataBase(BookList my_list) {
        ArrayList <Book> list = my_list.getEntireList();
        
         XMLEncoder config_xml;
                        try {

                            config_xml = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("dataBase.xml")));
                            Iterator i = list.iterator();
                            while(i.hasNext()){
                            config_xml.writeObject((Object) i.next());
                            config_xml.flush();
                            }
                            config_xml.close();
                        } catch (FileNotFoundException ex) {
                        }
    }
    
    public static void saveFields(ComboFields f){
        ArrayList <String> temp_list = f.getFields();
                
        XMLEncoder fields_xml;
                        try {

                            fields_xml = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("comboFields.xml")));
                            Iterator i = temp_list.iterator();
                            while(i.hasNext()){
                            fields_xml.writeObject((Object) i.next());
                            fields_xml.flush();
                            }
                            fields_xml.close();
                        } catch (FileNotFoundException ex) {
                        }
    }
}
